﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

using PM.Helper;
using PM.Entity;
using PM.Bussiness;
using PM.Repository;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace PM.Controllers
{
    /// <summary>
    /// 仓库
    /// </summary>
    [Route("api/[controller]")]
    [Authorize(Roles = "Admin")]
    public class StorageController : Controller
    {
        private StorageBLL BLL = new StorageBLL();
        private PaiGongDal paiGongDal = new PaiGongDal();

        /// <summary>
        /// 入库
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("In")]
        public JsonResult In([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            string position = para["position"].ToString();

            string uid = Request.Headers["Uid"].ToString();
            string result = BLL.In(barcode, position, uid,"");
            if (result.Equals("true"))
            {
                BarCode barCoDeBeen = paiGongDal.searchChanpin(barcode);
                var resulto = new
                {
                    recode = 1,
                    msg = "入库成功",
                    info = barCoDeBeen
                };
                return Json(resulto);
            }
            else
            {
                BarCode barCoDeBeen = null;
                return Json(new { recode = -1, msg = result, info = barCoDeBeen });
            }
        }
        /// <summary>
        /// 查询派工单
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("PaiGong")]
        public JsonResult PaiGong([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string paigongdan = para["paigongdan"].ToString();

            PaiGongdan paigongdanBeen = paiGongDal.SearchByDingdan(paigongdan);
            var result = new
            {
                recode = (paigongdanBeen == null ? -1 : 1),
                msg = (paigongdanBeen == null ? "没有数据" : "成功"),
                info = paigongdanBeen
            };

            return Json(result);
        }
        /// <summary>
        /// 查询chaixiang
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("searchChaixiang")]
        public JsonResult searchChaixiang([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();

            BarCode paigongdanBeen = paiGongDal.searchChanpinRecord(barcode);
            var result = new
            {
                recode = (paigongdanBeen == null ? -1 : 1),
                msg = (paigongdanBeen == null ? "没有数据或条码已拆箱" : "成功"),
                info = paigongdanBeen
            };

            return Json(result);
        }
        /// <summary>
        /// 拆箱
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("chaixiang")]
        public JsonResult chaixiang([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string tiaoma = para["tiaoma"].ToString();
            decimal weight1 = Decimal.Parse(para["weight1"].ToString());
            int shuliang1 = int.Parse(para["shuliang1"].ToString());
            decimal weight2 = Decimal.Parse(para["weight2"].ToString());
            int shuliang2 = int.Parse(para["shuliang2"].ToString());
            string uid = Request.Headers["Uid"].ToString();
            DateTime timer = DateTime.Now;
            string[] result = paiGongDal.insertChaiXiang(tiaoma, weight1, shuliang1, weight2, shuliang2, uid, timer);

            if (result != null)
            {

                var resulto = new
                {
                    recode = 1,
                    msg = "拆箱成功",
                    info = new
                    {
                        timer = timer.ToString(),
                        barode1 = result[0],
                        barode2 = result[1],
                    }
                };
                return Json(resulto);
            }
            else
            {

                return Json(new { recode = -1, msg = "拆箱失败", info = "" });

            }
        }





        /// <summary>
        /// 出库
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("Out")]
        public JsonResult Out([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            string paigonganId = para["paigonganId"].ToString();

            string uid = Request.Headers["Uid"].ToString();
            PaiGongDal paiGongDal = new PaiGongDal();
            string result = paiGongDal.outKuCun(barcode, paigonganId, uid,"");
            if (result.Equals("true"))
            {
                BarCode barCoDeBeen = paiGongDal.searchChanpin(barcode);
                var resulto = new
                {
                    recode = 1,
                    msg = "出库成功",
                    info = barCoDeBeen
                };
                return Json(resulto);
            }
            else
            {
                BarCode barCoDeBeen = null;
                return Json(new { recode = -1, msg = result, info = barCoDeBeen });

            }
        }
        /// <summary>
        /// 移库-查询条码信息
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("yksearch")]
        public JsonResult yksearch([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            BarCode barCoDeBeen = paiGongDal.searchChanpin(barcode);
            var resulto = new
            {
                recode = 1,
                msg = "",
                info = barCoDeBeen
            };
            return Json(resulto);
        }
        /// <summary>
        /// 移库
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("yiku")]
        public JsonResult yiku([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            string dycangku = para["dycangku"].ToString();
            string dycangwei = para["dycangwei"].ToString();
            string ykcangwei = para["ykcangwei"].ToString();

            string uid = Request.Headers["Uid"].ToString();
            //检查仓位
            仓库仓位 posEntity = new PositionDAL().SearchByPosition(ykcangwei,"");
            if (posEntity == null)
            {
                var resulto = new
                {
                    recode = -1,
                    msg = "仓位条码不正确",
                    info = new BarCode()
                };
                return Json(resulto);
            }
            if (!posEntity.仓库编号.Equals(dycangku))
            {
                var resulto = new
                {
                    recode = -1,
                    msg = "移动仓位仓库编号不一样",
                    info = new BarCode()
                };
                return Json(resulto);
            }
            if (paiGongDal.yiku(uid, dycangwei, barcode, posEntity.仓位编号, posEntity.仓库编号,""))
            {
                BarCode barCoDeBeen = paiGongDal.searchChanpin(barcode);
                var resulto = new
                {
                    recode = 1,
                    msg = "移库失败",
                    info = barCoDeBeen
                };
                return Json(resulto);
            }
            else
            {
                var resulto = new
                {
                    recode = -1,
                    msg = "移库失败",
                    info = new BarCode()
                };
                return Json(resulto);
            }

        }
        /// <summary>
        /// 查询仓库/区域/派工单产品
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("searhchanpin")]
        public JsonResult searhChanpin([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            List<BarCode> barCodeList = null;
            //查询派工单产品
            if (barcode.StartsWith("R"))
            {
                barCodeList = paiGongDal.searchByPaiGongdan(barcode);
            }
            //查询仓位产品
            else if (barcode.StartsWith("C"))
            {
                barcode = barcode.Substring(1);
                barCodeList = paiGongDal.searchByCangwei(barcode);
            } //查询区域产品
            else if (barcode.StartsWith("S"))
            {
                barcode = barcode.Substring(1);
                barCodeList = paiGongDal.searchByQuYu(barcode);
            }

            var resulto = new
            {
                recode = 1,
                msg = "",
                info = barCodeList
            };
            return Json(resulto);

        }

        /// <summary>
        /// 锁库
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("suoku")]
        public JsonResult suoku([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string uid = Request.Headers["Uid"].ToString();
            string quyu = para["quyu"].ToString();
           bool isSuoku= bool.Parse(para["isSuoku"].ToString());
            String pandianbianhao = para["pandianbianhao"].ToString();
            List<CangWeiReturn> cangweiList = null;
            List<BarCode> tiaomaList = null;
            if (isSuoku)
            {
                pandianbianhao=paiGongDal.getpandianDan();
            }
            if (paiGongDal.suoku(quyu, isSuoku, pandianbianhao, uid, false, 0, 0, 0))
            {
                if (isSuoku)
                {
                    cangweiList = paiGongDal.searchPanDianCangWei(quyu);
                    tiaomaList = paiGongDal.searchPanDianTiaoma(quyu);
                }
            }
            else {
                var resulto2 = new
                {
                    recode = -1,
                    msg = "",
                    info = new
                    {
                        pandian = pandianbianhao,
                        cangwei = cangweiList,
                        tiaoma = tiaomaList
                    }
                };
                return Json(resulto2);
            }
            var resulto = new
            {
                recode = 1,
                msg = "",
                info = new
                {
                    pandian = pandianbianhao,
                    cangwei = cangweiList,
                    tiaoma = tiaomaList
                }
            };
            return Json(resulto);
        }
        /// <summary>
        /// 根据产品条码查询产品信息 移库查询 信息
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("pandian")]
        public JsonResult pandian([FromBody]PanDianBeen panDianBeen)
        {
            string uid = Request.Headers["Uid"].ToString();
            List<PanDian>  panDianList=panDianBeen.rukuBeenList;
            List<PanDian> panDianInList = new List<PanDian>();
            List<PanDian> panDianOutList = new List<PanDian>();
            List<PanDian> panDianYikuList = new List<PanDian>();
            String pandianbianhao = panDianBeen.pandianbianhao;
            int tiaomaCount = 0;
            int pandianCount = 0;
            float panDianWeight = 0;
             List<String> errorList = new List<string>();
            for (int i=0;i<panDianList.Count;i++) {
                PanDian pandian=panDianList[i];
                if ("IN".Equals(pandian.isneedPanDian)) {
                    panDianInList.Add(pandian);
                }
                else
                if ("OUT".Equals(pandian.isneedPanDian))
                {
                    panDianOutList.Add(pandian);
                }
                else
                {
                    tiaomaCount++;
                    pandianCount = pandianCount + pandian.shuliang;
                    panDianWeight = panDianWeight + pandian.weight;
                    paiGongDal.liuzhuanjilu(uid,pandian.cptiaoma, pandianbianhao);
                }
                
                if (pandian.newCangwei!=null&&!"".Equals(pandian.newCangwei)) {
                    panDianYikuList.Add(pandian);
                }
            }
            if (panDianBeen.hasChange)
            {
               
            }
            else {
                for (int i = 0; i < panDianYikuList.Count; i++)
                {
                    PanDian pandian = panDianYikuList[i];
                    bool result = paiGongDal.yiku(uid, pandian.cangwei, pandian.cptiaoma, pandian.newCangwei, pandian.cangkubianhao, pandianbianhao);
                    if (result)
                    {

                    }
                    else
                    {
                        errorList.Add(pandian.cptiaoma);
                    }
                }
                for (int i = 0; i < panDianInList.Count; i++)
                {
                    PanDian pandian = panDianInList[i];
                    String cangwei = "";
                    if (pandian.newCangwei != null && !"".Equals(pandian.newCangwei))
                    {
                        cangwei = pandian.newCangwei;
                    }
                    else {
                        cangwei = pandian.cangwei;
                    }
                    string result = BLL.In(pandian.cptiaoma, cangwei, uid, pandianbianhao);
                    if ("true".Equals(result))
                    {
                        tiaomaCount++;
                        pandianCount = pandianCount + pandian.shuliang;
                        panDianWeight = panDianWeight + pandian.weight;
                    }
                    else {
                        errorList.Add(pandian.cptiaoma);
                    }
                }
                for (int i = 0; i < panDianOutList.Count; i++)
                {
                    PanDian pandian = panDianOutList[i];
                    string result = paiGongDal.outKuCun(pandian.cptiaoma, "", uid, pandianbianhao);
                    if ("true".Equals(result))
                    {

                    }
                    else
                    {
                        errorList.Add(pandian.cptiaoma);
                    }
                }
              
            }
            paiGongDal.suoku(panDianBeen.quyu, false, pandianbianhao, uid, true, tiaomaCount, pandianCount, Convert.ToDecimal(panDianWeight));
            if (errorList.Count > 0)
            {
                var resulto = new
                {
                    recode = -1,
                    msg = "部分盘点失败",
                    info = errorList
                };
                return Json(resulto);
            }
            else {
                var resulto = new
                {
                    recode = 1,
                    msg = "",
                    info = errorList
                };
                return Json(resulto);
            }
           
        }

        /// <summary>
        /// 查询外发拆箱
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("xcsearchChaixiang")]
        public JsonResult xianChansearchChaixiang([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();

            BarCode paigongdanBeen = paiGongDal.searchXianChanChanpinRecord(barcode);
            var result = new
            {
                recode = (paigongdanBeen == null ? -1 : 1),
                msg = (paigongdanBeen == null ? "没有数据或条码已拆箱" : "成功"),
                info = paigongdanBeen
            };

            return Json(result);
        }
        /// <summary>
        /// 外发拆箱
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("wfchaixiang")]
        public JsonResult wfchaixiang([FromBody]WfChaiXiangReq req)
        {
            string uid = Request.Headers["Uid"].ToString();
            DateTime timer = DateTime.Now;
            WfChaiXiangReq reqNew = paiGongDal.insertWfChaiXiang(req, uid, timer);

            if (reqNew != null)
            {
                reqNew.recode = 1;
                reqNew.msg = "拆箱成功";
                return Json(reqNew);
            }
            else
            {

                return Json(new { recode = -1, msg = "拆箱失败", wfChaXiangList = "" });

            }
        }

        /// <summary>
        /// 在制品入库
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>

        [HttpPost]
        [Route("ZaiZhiIn")]
        public JsonResult ZaiZhiIn([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            string position = para["position"].ToString();

            string uid = Request.Headers["Uid"].ToString();
            string result = paiGongDal.ZaiZhiIn(barcode, position, uid);
            if (result.Equals("true"))
            {
                BarCode barCoDeBeen = paiGongDal.searchChanpin(barcode);
                var resulto = new
                {
                    recode = 1,
                    msg = "入库成功",
                    info = barCoDeBeen
                };
                return Json(resulto);
            }
            else
            {
                BarCode barCoDeBeen = null;
                return Json(new { recode = -1, msg = result, info = barCoDeBeen });
            }
        }
        /// <summary>
        /// 查询在制产品根据派工单查询
        /// </summary>
        /// <param name="para"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("zaizhisearhchanpin")]
        public JsonResult zaizhisearhchanpin([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string barcode = para["barcode"].ToString();
            List<BarCode> barCodeList = null;
            //查询派工单产品
            if (barcode.StartsWith("R"))
            {
                barCodeList = paiGongDal.searchZaiZhiByPaiGongdan(barcode);
            }
            var resulto = new
            {
                recode = 1,
                msg = "",
                info = barCodeList
            };
            return Json(resulto);

        }
        
    }
}
